// Initialize your app
// var DEBUG_LOCAL = true;
//var SERVER_PATH = 'json/';
var WEB_CONTEXT_PATH = 'http://scut-oa.wisedu.com/sixx';
var SERVER_PATH = WEB_CONTEXT_PATH + '/mobile/';
var SCHEME_SERVER_PATH = 'http://scut-oa.wisedu.com';
//var SERVER_PATH = 'json/';
//var SERVER_PATH = 'http://172.16.40.25:8080/mobile/';//oaadmin/0
var PORTRAIT_PATH = WEB_CONTEXT_PATH+'/CONFIRMIMAGE/headportrait/';
var PAGE_SIZE = 20;
var CURRENT_USER;


//Framework7.prototype.plugins.hook = function (app, params) {
//    // exit if not enabled
//    if (!params) return;
//
//    return {
//        hooks: {
//            routerLoad: function (view, options) {
//                console.log ('routerLoad');
//            },
//            routerBack: function (view, options) {
//                console.log ('routerBack');
//            }
//        }
//    };
//};

var myApp = new Framework7({
    init: false, //Disable App's automatica initialization
    precompileTemplates: true,
    template7Pages: true, // enable Template7 rendering for Ajax and Dynamic pages
    template7Data: {}
});
myApp.padApp = true;
myApp.splitWindow = function(columns, resetWhenHide) {
    $$('body').removeClass('split-column-2');
    if (columns === 1) {
        if (resetWhenHide) {
            $$('li.selected-item').removeClass('selected-item');
            viewCenter.back({force: true, url: document.location.href, animatePages: false});
        }
    } else if (columns === 2) {
        $$('body').addClass('split-column-2');
    }
};

// Export selectors engine
var $$ = Dom7;

// Add views
var viewLeft = myApp.addView('#view-left', {
    dynamicNavbar: true,
    preroute: function(view, options) {
        if (options && options.preloadOnly) {
            return true;
        }

        var url = options.url;
        if (options && options.back) {
            url = view.history[view.history.length - 2];
        }
        url = url && url.split('?')[0];
        console.log('left preroute ' + url);
        if (url === document.location.href
            || url === 'addressBook-pad.html'
            || url === 'messageCenter.html'
            || url === 'quick-start.html'
            || url === 'meeting.html'
            || url === 'agendaItem-details.html') {
            myApp.splitWindow(1, true);
        } else if (url === 'selectUser-pad.html' || url === 'selectDepart-pad.html') {
            return true;
        } else if (url === 'agenda.html' || url === 'weekAgenda.html' || url === 'leadactInfo.html') {
            setTimeout(function() {
                viewLeft.router.load({url: 'calendar.html'});
                viewCenter.router.load(options);
            }, 0);
            return false;
        } else {
            myApp.splitWindow(2, true);
        }
    }
});
var viewCenter = myApp.addView('#view-center', {
    //uniqueHistory: true,
    //uniqueHistoryIgnoreGetParameters: true,
    dynamicNavbar: true,
    preroute: function(view, options) {
    //    if (options && options.normal) {
    //        return;
    //    }
    //    if (options && options.back) {
    //        options.force = true;
    //        options.url = document.location.href;
    //
    //        if ($$('body').hasClass('fullscreen')) {
    //            $$('body').removeClass('fullscreen');
    //            return false;
    //        }
    //    }
        if (options && options.preloadOnly) {
            return true;
        }

        var url = options.url;
        if (options && options.back) {
            url = view.history[view.history.length - 2];
        }
        url = url && url.split('?')[0];
        if (url === 'selectUser-pad.html' || url === 'selectDepart-pad.html') {
            $$('body').addClass('fullscreen-select');
        } else {
            $$('body').removeClass('fullscreen-select');
        }
    }
});

var viewPopup = myApp.addView('#view-popup', {
    dynamicNavbar: true,
    animatePages: false,
    preroute: function(view, options) {
        if (options.url && !options.back) {
            $$('body').addClass('with-view-popup');
        } else if (view.history.length == 1 && options.preloadOnly) {
            $$('body').removeClass('with-view-popup');
        }
    }
});
$$('.panel.panel-left').on('close', function() {
    viewPopup.back({force: true, url: document.location.href, animatePages: false});
});
$$('.close-view-popup').on('click', function() {
    viewPopup.back({force: true, url: document.location.href, animatePages: false});
});

$$(document).on('click', '.toggle-fullscreen', function () {
    $$('body').toggleClass('fullscreen');
    myApp.sizeNavbars(viewCenter.container);
});

$$(document).on('click', '.message-list li', function () {
    $$('li.selected-item').removeClass('selected-item');
    $$(this).addClass('selected-item');
});

$$(document).on('click', '.fast-menu-toggle', function () {
    var toggle = $$(this),
        menu = $$('.fast-menu');
    if (toggle.hasClass('fast-menu-open')) {
        toggle.removeClass('fast-menu-open');
        menu.removeClass('fast-menu-open');
    } else {
        toggle.addClass('fast-menu-open');
        menu.addClass('fast-menu-open');
    }
});
$$(document).on('click', '.fast-menu .category-expand', function () {
    var category = $$(this).parents('.menu-category'),
        items = category.find('.quick-menu-item');
    if (category.hasClass('category-expanded')) {
        category.removeClass('category-expanded');
        for (var i = 2; i < items.length; i++) {
            items.eq(i).hide();
        }
    } else {
        category.addClass('category-expanded');
        items.show();
    }
});

var homePage;
function refreshHome (page) {
    $$.getJSONP(SERVER_PATH + 'menuIndex4pad.do', null, function (json) {
        if (json.code == 1) {
            var ul, html, grid_slides = [];
            $$.each(json.results, function (index, value) {
                if (index % 12 === 0) {
                    if (ul) {
                        grid_slides.push({
                            id: 'grid-' + grid_slides.length,
                            content: ul.prop('outerHTML')
                        });
                    }
                    ul = $$('<ul class="grid-menu"></ul>');
                }

                html = '<li ><a class="menu-link" href="' + value.moduleUrl + '">' +
                    '<i class="icon" style="background-image:url(\'' + value.img + '\')">' +
                    (value.unReadCount > 0 ? '<span class="badge">' + (value.unReadCount > 99 ? '99+' : value.unReadCount) + '</span>' : '') + '</i>' +
                    '<span>' + value.moduleName + '</span>' +
                    '</a></li>';
                ul.append(html);
            });
            if (ul) {
                grid_slides.push({
                    id: 'grid-' + grid_slides.length,
                    content: ul.prop('outerHTML')
                });
            }

            var gridPager = $$(page.container).find('.page-content').data('grid-pager');
            if (gridPager) {
                gridPager.close();
            }
            var gridPager = myApp.welcomescreen(grid_slides, {
                target: $$(page.container).find('.page-content'),
                cssClass: 'grid-pager',
                closeButton: false
            });
            $$(page.container).find('.page-content').data('grid-pager', gridPager);

        }
    });

    $$.getJSONP(SERVER_PATH + 'quickIndex.do', null, function (json) {
        if (json.code == 1) {
            var count = json.results.length;
            $$('.fast-menu').children().remove();
            var flows = [], infos = [];
            for (var i = 0; i < count; i++) {
                var item = json.results[i];
                if (item.type === 'flow') {
                    flows.push(item);
                } else if (item.type === 'info') {
                    infos.push(item);
                }
            }

            function addCategory(title, array) {
                var i;
                if (array.length === 0) return;
                var ul = $$('<ul class="menu-category"><li class="category-title">' + title + '</li></ul>');
                for (i = 0; i < array.length; i++) {
                    var item = array[i];
                    ul.append('<li class="quick-menu-item">' +
                        '<a  class="menu-link" href="' + item.moduleUrl + '"><i class="icon icon-fast-menu" style="background-image: url(\'' + item.img +'\');"></i>' +
                        '<span>' + item.moduleName + '</span></a>' +
                        '</li>');
                }
                if (array.length > 2) {
                    ul.append('<li class="category-expand"><i class="icon icon-arrow"></i></li>');
                    var items = ul.find('.quick-menu-item');
                    for (i = 2; i < array.length; i++) {
                        items.eq(i).hide();
                    }
                }
                $$('.fast-menu').append(ul);
            }
            addCategory('流', flows);
            addCategory('信', infos);
        }
    });
}

myApp.onPageInit('login', function (page) {
    console.log("onPageInit " + page.name);

    //设置页面主题
    var themeName = localStorage.getItem('themeName');
    if(themeName){
        $('body').attr('data-theme', themeName);
    } else {
        $('body').attr('data-theme', '');
    }

    // login input css
    var inputs = $$('.login-screen input[type=text]').add('.login-screen input[type=password]');
    inputs.parents('.item-inner').css('border-bottom', '1px solid rgba(255, 255, 255, 0.3)');
    inputs.focusin(function () {
        $(this).parents('.item-inner').css('border-bottom', '1px solid #fff');
    }).focusout(function () {
        $(this).parents('.item-inner').css('border-bottom', '1px solid rgba(255, 255, 255, 0.3)');
    });

    var nameInput = $$(page.container).find('input[name="username"]'),
        pwdInput = $$(page.container).find('input[name="password"]'),
        rememberBox = $$(page.container).find('input[name="remember-checkbox"]'),
        autoLoginBox = $$(page.container).find('input[name="autologin-checkbox"]'),
        button = $$(page.container).find('.login-button'),
        logoutbutton = $$('.siderbar .logout-button'),
        transition = $$(page.container).find('.login-screen-transition');

    nameInput.val(localStorage.getItem('username'));
    rememberBox.prop('checked', localStorage.getItem('remember-password') == 'true');
    autoLoginBox.prop('checked', localStorage.getItem('auto-login') == 'true');
    if (rememberBox.prop('checked')) {
        pwdInput.val(localStorage.getItem('password'));
    }

    rememberBox.change(function () {
        var checked = $$(this).prop('checked');
        autoLoginBox.prop('disabled', !checked);
        autoLoginBox.parents('.label-checkbox').css('opacity', checked ? '1' : '0.5');
    }).change();

    nameInput.add(pwdInput).on('input', function () {
        if (nameInput.val() && pwdInput.val()) {
            button.removeClass('disabled');
        } else {
            button.addClass('disabled');
        }
    }).trigger('input');

    function onLoginSuccess(user) {

            CURRENT_USER = user;
            $$.getJSONP(SERVER_PATH + 'getPersonalInfo.do', null, function (json) {
                if (json.code == 1) {
                    $$.each(json.results, function (key, value) {
                        CURRENT_USER[key] = value;
                    });
                    $$('a.icon-user img').attr('src', CURRENT_USER.image);
                    $$('.siderbar .avart img').attr('src', CURRENT_USER.image);
                    $$('.siderbar .avart .username').text(CURRENT_USER.username);
                    $$('.siderbar .avart .userid').text(CURRENT_USER.loginid);
                }
            });

            if (homePage) {
                refreshHome(homePage);
            }

            transition.show();
            transition.prevAll().hide();

            var width = $$(page.container).width(),
                height = $$(page.container).height(),
                x = (width - transition.outerWidth()) / 2,
                y = (height - transition.outerHeight()) / 2;
            transition.css({
                left: x + 'px',
                top: y + 'px',
                opacity: 0.01
            });
            transition.progress = 0.01;

            var animId;
            function zoomOut() {
                transition.progress += 0.02;
                transition.transform('scale(' + transition.progress + ')');
                transition.css({opacity: transition.progress});
                if (transition.progress < 1) {
                    animId = $$.requestAnimationFrame(zoomOut);
                } else {
                    myApp.closeModal('.login-screen.modal-in');
                }
            }
            animId = $$.requestAnimationFrame(zoomOut);

    }

    button.on('click', function () {
        var username = nameInput.val(),
            password = pwdInput.val();

        if (username.length == 0) {
            myApp.toast('请输入用户名', '<div>×</div>', {}).show(true);
            return;
        }
        if (password.length == 0) {
            myApp.toast('请输入密码', '<div>×</div>', {}).show(true);
            return;
        }

        localStorage.setItem('username', username);
        localStorage.setItem('remember-password', rememberBox.prop('checked'));
        localStorage.setItem('auto-login', autoLoginBox.prop('checked'));
        if (rememberBox.prop('checked')) {
            localStorage.setItem('password', password);
        } else {
            localStorage.removeItem('password');
        }

        myApp.showIndicator();
        $$.getJSONP(SERVER_PATH + 'mobileLogin.do', {loginname: username, password: password}, function (json) {
            myApp.hideIndicator();
            if (json.code == 1) {
                onLoginSuccess(json.results);
            } else {
                myApp.toast(json.msg, '<div>×</div>', {}).show(true);
            }
        });
    });

    //注销账户
    logoutbutton.on('click', function () {
        console.log("logout");

        localStorage.removeItem('username');
        localStorage.removeItem('password');
        localStorage.removeItem('remember-password');
        localStorage.removeItem('auto-login');
        //CURRENT_USER = null;

        window.location.reload();
        //myApp.mainView.router.refreshPage();
    });

    if(typeof clientType!=="undefined"){
        logoutbutton.hide();
        localStorage.removeItem('username');
        localStorage.removeItem('password');
        localStorage.removeItem('remember-password');
        localStorage.removeItem('auto-login');
        if(clientType==1){
            document.addEventListener("deviceready", function(){
                TrunPage.getPassword(function(data){
                    if(data){
                        TrunPage.getUserName(function(data){
                            if (data) {
                                transition.prevAll().hide();
                                myApp.showIndicator();
                                $$.getJSONP(SERVER_PATH + 'mobileLoginForIsh.do', {loginname: data}, function (json) {
                                    myApp.hideIndicator();
                                    if (json.code == 1) {
                                        onLoginSuccess(json.results);
                                    } else {
                                        transition.prevAll().show();
                                    }
                                });
                            }
                        });
                    }
                });
            }, false);
        }else{
            TrunPage.getPassword(function(data){
                if(data){
                    TrunPage.getUserName(function(data){
                        if (data) {
                            transition.prevAll().hide();
                            myApp.showIndicator();
                            $$.getJSONP(SERVER_PATH + 'mobileLoginForIsh.do', {loginname: data}, function (json) {
                                myApp.hideIndicator();
                                if (json.code == 1) {
                                    onLoginSuccess(json.results);
                                } else {
                                    transition.prevAll().show();
                                }
                            });
                        }
                    });
                }
            });
        }
    }else{
        $$(page.container).find('.login-screen-content').show();
        if(autoLoginBox.prop('checked')){
            button.click();
        }
    }
});

$("#fast-menu").wheelmenu({
    trigger: "click",
    animation: "fade",
    animationSpeed: "fast"
});

myApp.onPageInit('home', function (page) {
    console.log("onPageInit " + page.name);

    homePage = page;
    if (CURRENT_USER) {
        refreshHome(homePage);
    }
});
myApp.onPageAfterAnimation('home', function (page) {
    console.log("onPageAfterAnimation " + page.name);
    if (CURRENT_USER) {
        refreshHome(homePage);
    }
});
myApp.onPageBeforeRemove('home', function (page) {
    var gridPager = $$(page.container).find('.page-content').data('grid-pager');
    if (gridPager) {
        gridPager.close();
    }
});

//动态导航栏刷新头像
$$(document).on('navbarInit', function (e) {
	var navbar = e.detail.navbar;
	var navbarCon = $$(navbar.container);

	if(CURRENT_USER){
		navbarCon.find('.icon-user img').attr('src', CURRENT_USER.image);
	}
});

myApp.init();


function preprocessData(type, value) {
    if (type === 'workItemList') {
        value.portrait = PORTRAIT_PATH + value.senduserid + '.jpg';
        value.taskcreatedate = value.taskcreatedate.substring(0, 16);
    } else if (type === 'userAndJobAllProcessTaskList') {
        value.portrait = PORTRAIT_PATH + value.senduserid + '.jpg';
        value.startdate = value.startdate.substring(0, 16);
    } else if (type === 'docRelayList') {
        value.portrait = PORTRAIT_PATH + value.docRelay.creatorId + '.jpg';
        value.sponsor_time = value.sponsor_time.substring(0, 16);
    } else if (type === 'docRelayDoneList') {
        value.portrait = PORTRAIT_PATH + value.docRelay.creatorId + '.jpg';
        value.oaDocReceiver = value.oaDocReceiver.substring(0, 16);
    } else if (type === 'applicationStatusList') {
        if (value.formid == 'mangoMeetingApply') {//会议
            value.iconClass = 'icon-caption';
        } else if (value.formid == 'topicApply') {//议题
            value.iconClass = 'icon-issue';
        } else if (value.formid == 'topicCollect') {//议题汇总
            value.iconClass = 'icon-allissue';
        } else if (value.formid == 'weekDetailApply') {//周会表
            value.iconClass = 'icon-weeklymeeting';
        } else {//系统表单是数字
            value.iconClass = 'icon-form';
        }

        value.uuid = $$.uniqueName();
        myApp.template7Data[value.uuid] = value;
    } else if (type === 'xxwjList') {
    } else if (type === 'announcementList') {
        value.publishDate = value.publishDate.substring(0, 16);
    } else if (type === 'MessageCenter') {
    }
}
function getDataTemplate(type) {
    var template;
    if (type === 'workItemList') {
        template = Template7.templates.workItemTemplate;
    } else if (type === 'userAndJobAllProcessTaskList') {
        template = Template7.templates.userAndJobAllProcessTaskTemplate;
    } else if (type === 'docRelayList') {
        template = Template7.templates.relayItemTemplate;
    } else if (type === 'docRelayDoneList') {
        template = Template7.templates.relayDoneItemTemplate;
    } else if (type === 'applicationStatusList') {
        template = Template7.templates.applyStatusTemplate;
    } else if (type === 'xxwjList') {
        template = Template7.templates.xxwjTemplate;
    } else if (type === 'announcementList') {
        template = Template7.templates.announcementTemplate;
    } else if (type === 'MessageCenter') {
        template = Template7.templates.messageTemplate;
    }
    return template;
}


// Templates
Template7.templates.workItemTemplate = Template7.compile(
    '<li><a href="workItem-details.html?id={{id}}&wt={{wt}}" data-view="#view-center" class="item-link item-content">' +
    '<div class="item-media"><img class="icon icon-portrait" src="{{portrait}}"></div>' +
    '<div class="item-inner">' +
    '<div class="item-title-row">' +
    '<div class="item-title">{{#js_compare "this.hjId > 0 && this.hjType != \'General\'"}}<span style="color: #ef8333">！</span>{{/js_compare}}{{processinstancetitle}}</div>' +
    '{{#js_compare "this.unReadWorkItem == 0"}}<div class="item-after"><span class="badge">New</span></div>{{/js_compare}}' +
    '</div>' +
    '<div class="item-title-row"><div class="item-subtitle">【{{processname}}】</div><div class="item-after">{{taskcreatedate}}</div></div>' +
    '</div>' +
    '</a></li>');
Template7.templates.relayItemTemplate = Template7.compile(
    '<li><a href="docRelay-details.html?docRelayId={{docRelay.rootId}}" data-view="#view-center" class="item-link item-content">' +
    '<div class="item-media"><img class="icon icon-portrait" src="{{portrait}}"></div>' +
    '<div class="item-inner">' +
    '<div class="item-title-row">' +
    '<div class="item-title">{{docRelay.docTitle}}</div>' +
    '{{#js_compare "this.unReadDocrelay == 0"}}<div class="item-after"><span class="badge">New</span></div>{{/js_compare}}' +
    '</div>' +
    '<div class="item-title-row"><div class="item-subtitle">【{{processname}}】</div><div class="item-after">{{sponsor_time}}</div></div>' +
    '</div>' +
    '</a></li>');
Template7.templates.relayDoneItemTemplate = Template7.compile(
    '<li><a href="docRelayDone-details.html?docRelayId={{docRelay.rootId}}" data-view="#view-center" class="item-link item-content">' +
    '<div class="item-media"><img class="icon icon-portrait" src="{{portrait}}"></div>' +
    '<div class="item-inner">' +
    '<div class="item-title">{{docRelay.docTitle}}</div>' +
    '<div class="item-title-row"><div class="item-subtitle">【{{processname}}】</div><div class="item-after">{{oaDocReceiver}}</div></div>' +
    '</div>' +
    '</a></li>');
Template7.templates.userAndJobAllProcessTaskTemplate = Template7.compile(
    '<li><a href="userAndJobAllProcessTask-details.html?processinstanceid={{processinstanceid}}" data-view="#view-center" class="item-link item-content">' +
    '<div class="item-media"><img class="icon icon-portrait" src="{{portrait}}"></div>' +
    '<div class="item-inner">' +
    '<div class="item-title">{{processinstancetitle}}</div>' +
    '<div class="item-title-row"><div class="item-subtitle">【{{processname}}】</div><div class="item-after">{{startdate}}</div></div>' +
    '</div>' +
    '</a></li>');

Template7.templates.announcementTemplate = Template7.compile(
    '<li data-id="{{announcementInfos[0].pkId}}"><a href="announcement-details.html?pkId={{announcementInfos[0].pkId}}" data-view="#view-center" class="item-link item-content">' +
    '<div class="item-inner">' +
    '<div class="item-title-row">' +
    '<div class="item-title">{{#if announcementInfos[0].topFlag}}<span class="red">【置顶】</span>{{/if}}{{announcementInfos[0].title}}</div>' +
    '{{#if announcementInfos[0].isRead}}{{else}}<div class="item-after"><span class="badge">New</span></div>{{/if}}' +
    '</div>' +
    '<div class="item-title-row"><div class="item-subtitle">发布人：{{announcementInfos[0].publishUserName}}</div><div class="item-after">{{publishDate}}</div></div>' +
    '</div>' +
    '</a></li>');
Template7.templates.xxwjTemplate = Template7.compile(
    '<li><a href="xxwj-details.html?id={{id}}" data-view="#view-center" class="item-link item-content">' +
    '<div class="item-inner">' +
    '<div class="item-title-row"><div class="item-title">{{title}}{{#if hasAtt}}<i class="icon icon-attachment"></i>{{/if}}</div>{{#if isRead}}{{else}}<div class="item-after"><span class="badge">New</span></div>{{/if}}</div>' +
        //'<div class="item-title-row"><div class="item-subtitle">发件人：{{publishUser}}</div><div class="item-after">发布时间：{{publishDate}}</div></div>' +
    '<div class="item-subtitle">发件人：{{publishUser}}</div><div class="item-after" style="float: right; font-size: 13px;">{{publishDate}}</div>' +
    '</div>' +
    '</a></li>');
Template7.templates.messageTemplate = Template7.compile(
    '<li><a href="#" class="item-link item-content">' +
    '<div class="item-inner">' +
    '<div class="item-title-row">' +
    '<div class="item-title">{{title}}</div>' +
    '{{#js_compare "this.unReadMessage == 0"}}<div class="item-after"><span class="badge">New</span></div>{{/js_compare}}' +
    '</div>' +
    '<div class="item-subtitle" style="text-align: right">{{time}}</div>' +
    '</div>' +
    '</a></li>');

Template7.templates.applyStatusTemplate = Template7.compile(
    '<li><a href="applicationStatus-details.html?uuid={{uuid}}" data-view="#view-center" class="item-link item-content">' +
    '<div class="item-media"><i class="icon {{iconClass}}"></i></div>' +
    '<div class="item-inner">' +
    '<div class="item-title-row"><div class="item-title">{{processinstancetitle}}</div>{{#js_compare "this.applyStatus == 1"}}<div class="item-after status-success"><i class="icon"></i>申请成功</div>{{else}}<div class="item-after status-process">申请中</div>{{/js_compare}}</div>' +
    '<div class="item-subtitle">【{{processname}}】</div>' +
    '{{#if showInfo}}{{#if showInfo[0]}}<div class="item-subtitle">使用时间：{{showInfo[0].beginTime}}{{#if showInfo[0].beginTime}}</div><div class="item-subtitle" style="padding-left: 60px;">～{{/if}}{{showInfo[0].endTime}}</div>' +
    '<div class="item-subtitle">会议室：{{showInfo[0].meetingRoom}}</div>' +
    '<div class="item-subtitle">会议申请人：{{showInfo[0].applicant}}</div>{{/if}}{{/if}}' +
    '</div>' +
    '</a></li>');
Template7.templates.applyConfirmTemplate = Template7.compile(
    '<li class="item-content item-timedot">' +
    '<div class="item-media"><span>{{index}}</span></div>' +
    '<div class="item-inner">' +
    '<div class="item-subtitle">审批时间：{{confirmdate}}</div>' +
    '<div class="item-subtitle">审批人：{{confirmusername}}</div>' +
    '<div class="item-subtitle">审批意见：{{confirminfo}}</div>' +
    '</div>' +
    '</li>');

Template7.templates.fileTemplate = Template7.compile(
    '<li>' +
    '<a href="#" data-type="{{suffix}}" data-name="{{name}}" data-id="{{attachId}}" class="item-content item-link file-link">' +
    '<div class="item-media">' +
    '<i class="icon icon-{{suffix}}"></i>' +
    '</div>' +
    '<div class="item-inner">' +
    '<div class="item-title">{{name}}</div>' +
    '<div class="item-after"><i class="icon {{#if canPreview}}icon-preview{{else}}icon-download{{/if}}"></i></div>' +
    '</div>' +
    '</a>' +
    '</li>');
$$(document).on('click', 'a.file-link', function (e) {
    e.preventDefault();
    console.log('click ' + $$(this).attr('data-id'));
    var name = $$(this).attr('data-name');
    var type = $$(this).attr('data-type');
    var attid = $$(this).attr('data-id');
    var downloadurl = WEB_CONTEXT_PATH + "/downloadattachment/" + attid + "/" + name + "?iPlanetDirectoryPro=" + CURRENT_USER.iPlanetDirectoryPro;
    if(clientType==1){
        document.addEventListener("deviceready", function(){
            name = name.substr(0,name.indexOf("."+type));
            TrunPage.openAttachment(downloadurl,name,type);
        }, false);
    }else{
        name = name.substr(0,name.indexOf("."+type));
        TrunPage.openAttachment(downloadurl,name,type);
    }
});

Template7.templates.agendaTemplate = Template7.compile(
    '<li class="swipeout">' +
    '<a href="#" data-pkId="{{pkId}}" class="item-link swipeout-content item-content">' +
    '<div class="agenda-time">' +
    '<p>{{beginDate}}</p><p>{{endDate}}</p>' +
    '</div>' +
    '<div class="item-inner">' +
    '<div class="item-title line-clamp">{{title}}</div>' +
    '<form class="star-rating-display">' +
    '<input name="{{id}}" {{#js_compare "this.grade == 1"}}checked="checked"{{/js_compare}} type="radio" class="star" disabled readonly/>'+
    '<input name="{{id}}" {{#js_compare "this.grade == 2"}}checked="checked"{{/js_compare}} type="radio" class="star" disabled readonly/>'+
    '<input name="{{id}}" {{#js_compare "this.grade == 3"}}checked="checked"{{/js_compare}} type="radio" class="star" disabled readonly/>'+
    '<input name="{{id}}" {{#js_compare "this.grade == 4"}}checked="checked"{{/js_compare}} type="radio" class="star" disabled readonly/>'+
    '<input name="{{id}}" {{#js_compare "this.grade == 5"}}checked="checked"{{/js_compare}} type="radio" class="star" disabled readonly/>'+
    '</form>' +
    '</div>' +
    '</a>' +
    '<div class="swipeout-actions-right"><a href="#" class="swipeout-delete">删除</a></div>' +
    '</li>');
Template7.templates.leaderAgendaTemplate = Template7.compile(
    '<li><a href="leaderAgenda-details.html?pkId={{pkId}}" class="item-link item-content">' +
    '<div class="agenda-time">' +
    '<p>{{beginDate}}</p><p>{{endDate}}</p>' +
    '</div>' +
    '<div class="item-inner">' +
    '<div class="item-title line-clamp">{{title}}</div>' +
    '<form class="star-rating-display">' +
    '<input name="{{id}}" {{#js_compare "this.grade == 1"}}checked="checked"{{/js_compare}} type="radio" class="star" disabled readonly/>'+
    '<input name="{{id}}" {{#js_compare "this.grade == 2"}}checked="checked"{{/js_compare}} type="radio" class="star" disabled readonly/>'+
    '<input name="{{id}}" {{#js_compare "this.grade == 3"}}checked="checked"{{/js_compare}} type="radio" class="star" disabled readonly/>'+
    '<input name="{{id}}" {{#js_compare "this.grade == 4"}}checked="checked"{{/js_compare}} type="radio" class="star" disabled readonly/>'+
    '<input name="{{id}}" {{#js_compare "this.grade == 5"}}checked="checked"{{/js_compare}} type="radio" class="star" disabled readonly/>'+
    '</form>' +
    '</div>' +
    '<div class="agenda-after"><p>{{leadactName}}</p></div>' +
    '</a></li>');
Template7.templates.weekAgendaTemplate = Template7.compile(
    '<li><a href="#" class="item-link item-content">' +
    '<div class="agenda-time">' +
    '<p>{{startTime}}</p><p>{{endTime}}</p>' +
    '</div>' +
    '<div class="item-inner">' +
    '<div class="item-title line-clamp">{{title}}</div>' +
        //'<div class="item-title-row"><div class="item-subtitle">主持人: {{linkMan}}</div><div class="item-after">参加部门: {{mainDepart}}</div></div></div>' +
    '<div class="item-subtitle line-clamp">主持人: {{linkMan}}</div>' +
    '<div class="item-subtitle line-clamp">参加部门: {{mainDepart}}</div>' +
    '</div>' +
    '<div class="agenda-after">' +
        //'<p>会议室</p>' +  //省去“会议室”几个字
    '<p>{{roomName}}</p>' +
    '</div>' +
    '</a></li>');